/**
 * User: hou wanchun
 * Date: Feb 28, 2003
 * Time: 2:55:11 PM
 */
package com.ecomm.util.dbadapter;

import com.ecomm.exception.DBException;
import com.ecomm.util.Config;
import com.ecomm.util.DAO;
import com.ecomm.util.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 使用jdbc直接连接的方式实现的DAO
 * 使用配置文件database.jdbcdirect部分的信息初始化
 * 配置文件的参考例子：
 * database.jdbcdirect.daoadapterclass=com.ecomm.util.dbadapter.JDBCDAO
 * database.jdbcdirect.driver=com.newatlanta.jturbo.driver.Driver
 * database.jdbcdirect.url=jdbc:JTurbo://160.160.160.193:1433/oademo
 * database.jdbcdirect.username=sa
 * database.jdbcdirect.password=
 */
public class MediaJDBCDAO implements DAO
{
    private static Log log = Logger.getLog(MediaJDBCDAO.class);

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static
    {
        Configuration config = Config.subset("database.media.jdbcdirect");
        driver = config.getString("driver");
        url = config.getString("url");
        username = config.getString("username");
        password = config.getString("password", "");
    }

    /**
     * 取得一个数据库连接
     *
     * @return
     * @throws com.ecomm.exception.DBException
     */
    public Connection getConnection() throws DBException
    {
        Connection conn = null;
        try
        {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        }
        catch (Exception e)
        {
            log.error("不能获得一个数据库连接。", e);
            throw new DBException("不能获得一个数据库连接。错误信息是" + e.getMessage());
        }

        return conn;
    }

}
